package jp.tkym.labs.store.sql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

class PreparedStatementBindHelper{
	void set(int parameterIndex, PreparedStatement ps, Class<?> classType, Object value) throws SQLException{
		if(classType.equals(String.class)) ps.setString(parameterIndex, (String)value);
		if(classType.equals(int.class)) ps.setInt(parameterIndex, (Integer)value);
		if(classType.equals(Integer.class)) ps.setInt(parameterIndex, (Integer)value);
		if(classType.equals(long.class)) ps.setLong(parameterIndex, (Long)value);
		if(classType.equals(Long.class)) ps.setLong(parameterIndex, (Long)value);
		if(classType.equals(short.class)) ps.setShort(parameterIndex, (Short)value);
		if(classType.equals(Short.class)) ps.setShort(parameterIndex, (Short)value);
		if(classType.equals(double.class)) ps.setDouble(parameterIndex, (Double)value);
		if(classType.equals(Double.class)) ps.setDouble(parameterIndex, (Double)value);
		if(classType.equals(float.class)) ps.setFloat(parameterIndex, (Float)value);
		if(classType.equals(Float.class)) ps.setFloat(parameterIndex, (Float)value);
		if(classType.equals(byte.class)) ps.setByte(parameterIndex, (Byte)value);
		if(classType.equals(Byte.class)) ps.setByte(parameterIndex, (Byte)value);
		if(classType.equals(Date.class)){
			long time = ((Date)value).getTime();
			ps.setDate(parameterIndex, new java.sql.Date(time));
		}
	}
	
	Object get(String columnName, Class<?> classType, ResultSet rs) throws SQLException{
		try {
			if(classType.equals(String.class)) return rs.getString(columnName);
			if(classType.equals(int.class)) return rs.getInt(columnName);
			if(classType.equals(Integer.class)) return rs.getInt(columnName);
			if(classType.equals(long.class)) return rs.getLong(columnName);
			if(classType.equals(Long.class)) return rs.getLong(columnName);
			if(classType.equals(short.class)) return rs.getShort(columnName);
			if(classType.equals(Short.class)) return rs.getShort(columnName);
			if(classType.equals(double.class)) return rs.getDouble(columnName);
			if(classType.equals(Double.class)) return rs.getDouble(columnName);
			if(classType.equals(float.class)) return rs.getFloat(columnName);
			if(classType.equals(Float.class)) return rs.getFloat(columnName);
			if(classType.equals(byte.class)) return rs.getByte(columnName);
			if(classType.equals(Byte.class)) return rs.getByte(columnName);
			if(classType.equals(Date.class)){
				long time = rs.getDate(columnName).getTime();
				return new Date(time);
			}
		} catch (NullPointerException e) {
			String msg = 
				"NullPointer !! on get()" +
				"columnName="+columnName+
				", classType="+classType.getName();
			throw new IllegalArgumentException(msg, e);
		}
		return null;
	} 
}